const App = getApp()
import { url } from '../../modelApi/url_config.js'
Page({
    data: {
        statusBarHeight: "",
        statusBarHeight: '',
        isUserAuth: true, //用户允许
        addrlist: [],
        showselectbtn: false,
        selectaddressId: false,
    },
    onLoad: function (options) {
        this.setData({
            statusBarHeight: App.globalData.navHeight,
            statusBarHeight: App.globalData.navHeight,
            selectaddressId: options.addressId ? options.addressId : null
        });
    },
    onShow: function () {
        const pages = getCurrentPages()
        const route = pages.length >= 2 ? pages[pages.length - 2].route : 0
        this.setData({
            showselectbtn: route && (route.indexOf('pages/mine/sonpages/order/orderSettlement/index') > -1 || route.indexOf('pages/orderDetail/index') > -1)
        })
        this.getaddrlistFunc()
    },
    getaddrlistFunc() {
        App.ajax(url.getaddrlist, {}, 'get').then(res => {
            if (+res.code === 0 && res.data) {
                const data = res.data
                data.forEach(el => {
                    if (+el.id === +this.data.selectaddressId) {
                        el.selected = true;
                    }
                })
                this.setData({
                    addrlist: data
                })
            }
        })
    },
    getweixinAddress() {
        var e = this;
        wx.getSetting({
            success: function (s) {
                e.setData({
                    isUserAuth: s.authSetting["scope.address"]
                });
            }
        });
        wx.chooseAddress({
            success: (res) => {
                this.savewinarr(res)
            }
        })
    },
    savewinarr(res) {
        const par = {
            province: res.provinceName,
            city: res.cityName,
            distrect: res.countyName,
            address: res.detailInfo,
            recName: res.userName,
            phone: res.telNumber,
            postCode: res.postalCode,
            isDef: '1'
        }
        App.ajax(url.savelist, par, 'POST').then(res => {
            if (+res.code === 0) {
                this.getaddrlistFunc()
            }
        })
    },
    cancelAuth: function () {
        this.setData({
            isUserAuth: true
        });
    },
    setAuth: function () {
        this.setData({
            isUserAuth: true
        });
    },
    newaddrFunc() {
        wx.navigateTo({
            url: '/pages/newAddress/index',
        })
    },
    toManaorder(e) {
        const id = e.currentTarget.dataset.id
        const pages = getCurrentPages();
        const prevPage = pages[pages.length - 2];
        prevPage.getaddrpageAddr(id)
        wx.navigateBack()
    },
    toeditor(e) {
        wx.navigateTo({
            url: `/pages/newAddress/index?id=${e.currentTarget.dataset.id}`,
        })
    }
})